package com.neusoft.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.tribes.group.interceptors.TwoPhaseCommitInterceptor.MapEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.neusoft.bean.PageInfo;
import com.neusoft.bean.PageRecordInfo;
import com.neusoft.bean.TabBbsComment;
import com.neusoft.bean.TabBbsTopicinfo;
import com.neusoft.bean.TabBbsUserinfo;
import com.neusoft.service.IJieService;
import com.neusoft.util.StringDateUtil;

@Controller
@RequestMapping("/jie")
public class JieController {
	
	@Autowired
	private IJieService iJieService;
	
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	public String add(HttpServletRequest request){
		HttpSession session = request.getSession();
		TabBbsUserinfo userinfo = (TabBbsUserinfo) session.getAttribute("userinfo");
		if (userinfo == null) {
			return "redirect:/user/login";
		}else {
			return "jie/add";
		}
		
	}
	
	
	/**
	 * 1、发布帖子
	 * @param request
	 * @param tabBbsTopicinfo
	 * @return
	 */
	@RequestMapping("/addTopic")
	public String addTopic(HttpServletRequest request,TabBbsTopicinfo tabBbsTopicinfo) {
		HttpSession session = request.getSession();
		TabBbsUserinfo userinfo = (TabBbsUserinfo) session.getAttribute("userinfo");
		tabBbsTopicinfo.setUserid(userinfo.getId());
		iJieService.addTopic(tabBbsTopicinfo);
		return "redirect:/";
	}
	
	
	/**
	 * 1、查看帖子详细信息
	 * 2、获取评论
	 * 3、实现自增浏览次数
	 * @return
	 */
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable("id") int id) {
		iJieService.increase_view_count(id);
		System.out.println(id);
		Map<String, Object> mapTopic = iJieService.getTopicById(id);
		//下面的是map集合的遍历方式
		for(Map.Entry<String, Object> entry:mapTopic.entrySet()){
			if ("createtime".equals(entry.getKey())) {
				String dateStr = StringDateUtil.getStringDate((Date) entry.getValue());
				entry.setValue(dateStr);
			}
			
		}
		
		
		List<Map<String, Object>> lstComments = iJieService.getCommentsById(id);
		//这个是评论的遍历
		for (Map<String, Object> map:lstComments) {
			for(Map.Entry<String, Object> entry:map.entrySet()){
				if ("commentTime".equals(entry.getKey())) {
					String dateStr = StringDateUtil.getStringDate((Date) entry.getValue());
					entry.setValue(dateStr);
				}
				
			}
		}
		
		
		ModelAndView modelAndView = new ModelAndView();
		//下面数据共享
		modelAndView.addObject("mapTopic", mapTopic);
		modelAndView.addObject("lstComments", lstComments);
		//设置返回
		modelAndView.setViewName("jie/detail");
		return modelAndView;
	}
	
	/*
	@RequestMapping("/reply/{id}")
	public String reply(@PathVariable("id") int id,TabBbsComment tabBbsComment) {
		
		return null;
	}
	*/
	
	
	/**
	 * 评论帖子
	 * @param request
	 * @param tabBbsComment
	 * @return
	 */
	@RequestMapping("/reply")
	public String reply(HttpServletRequest request,TabBbsComment tabBbsComment) {
		HttpSession session = request.getSession();
		TabBbsUserinfo userinfo = (TabBbsUserinfo) session.getAttribute("userinfo");
		tabBbsComment.setUserid(userinfo.getId());
		iJieService.reply(tabBbsComment);
//		System.out.println(tabBbsComment.getContent());
//		System.out.println(tabBbsComment.getTopicId());
		return "redirect:/jie/detail/"+tabBbsComment.getTopicId();
		//此处用到拼串
	}
	
	
	
	/**
	 * 帖子分页浏览
	 * @return
	 */
	@RequestMapping("/index")
	public String index(){
		return "jie/index";
	}
	
	
	
	
	/**
	 * 拿到总的帖子条数的方法
	 */
	@RequestMapping("/get_topic_count")
	public void get_topic_count(HttpServletResponse response){
		int count = iJieService.get_topic_count();
		System.out.println(count);
		try {
			//这样那边就接收到了，都给jie下的index
			PrintWriter out = response.getWriter();
			out.println(count);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	/**
	 * 分页用的
	 */
	@RequestMapping("/get_paged_topics")
	public void get_paged_topics(PageInfo pageInfo,HttpServletResponse response) {
		//现在要送到数据库中做查询
		List<Map<String, Object>> lstTopics = iJieService.get_paged_topics(pageInfo);
		System.out.println(lstTopics.size());
		
		PageRecordInfo pageRecordInfo = new PageRecordInfo();
		pageRecordInfo.setList(lstTopics);
		
		//转成json类型字符串，键值绑定的。那边好解析
		String jsonStr = JSON.toJSONString(pageRecordInfo);
		
		try {
			PrintWriter out = response.getWriter();
			//把jsonstr输出出去，给到index   json字符串
			out.println(jsonStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	

}
